package com.camera.yl.v.data

import android.util.TypedValue
import android.widget.TextView
import androidx.databinding.BindingAdapter

/**
 * 水印相关的数据绑定适配器
 */
object WatermarkBindingAdapter {
    
    /**
     * 处理TextView的字体大小绑定
     * @param view TextView控件
     * @param fontSize 字体大小(整数)
     */
    @JvmStatic
    @BindingAdapter("android:textSize")
    fun setTextSize(view: TextView, fontSize: Int) {
        view.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSize.toFloat())
    }
    
    /**
     * 处理TextView的左边距绑定
     * @param view TextView控件
     * @param marginStart 左边距(整数)
     */
    @JvmStatic
    @BindingAdapter("android:layout_marginStart")
    fun setMarginStart(view: TextView, marginStart: Int) {
        val layoutParams = view.layoutParams as? android.view.ViewGroup.MarginLayoutParams ?: return
        layoutParams.marginStart = marginStart
        view.layoutParams = layoutParams
    }
    
    /**
     * 处理TextView的上边距绑定
     * @param view TextView控件
     * @param marginTop 上边距(整数)
     */
    @JvmStatic
    @BindingAdapter("android:layout_marginTop")
    fun setMarginTop(view: TextView, marginTop: Int) {
        val layoutParams = view.layoutParams as? android.view.ViewGroup.MarginLayoutParams ?: return
        layoutParams.topMargin = marginTop
        view.layoutParams = layoutParams
    }
    
    /**
     * 将百分比左边距转换为实际像素
     * @param view TextView控件
     * @param marginPercent 左边距百分比(0-100)
     */
    @JvmStatic
    @BindingAdapter("marginStartPercent")
    fun setMarginStartPercent(view: TextView, marginPercent: Int) {
        val parent = view.parent as? android.view.ViewGroup ?: return
        val layoutParams = view.layoutParams as? android.view.ViewGroup.MarginLayoutParams ?: return
        
        // 计算父容器宽度的百分比
        val actualMargin = (parent.width * marginPercent / 100)
        
        // 更新布局参数
        if (layoutParams.marginStart != actualMargin) {
            layoutParams.marginStart = actualMargin
            view.layoutParams = layoutParams
        }
    }
    
    /**
     * 将百分比右边距转换为实际像素
     * @param view TextView控件
     * @param marginPercent 右边距百分比(0-100)
     */
    @JvmStatic
    @BindingAdapter("marginEndPercent")
    fun setMarginEndPercent(view: TextView, marginPercent: Int) {
        val parent = view.parent as? android.view.View ?: return
        val layoutParams = view.layoutParams as? android.view.ViewGroup.MarginLayoutParams ?: return
        val actualMargin = (parent.width * marginPercent / 100)
        layoutParams.marginEnd = actualMargin
        view.layoutParams = layoutParams
    }
    
    /**
     * 将百分比上边距转换为实际像素
     * @param view TextView控件
     * @param marginPercent 上边距百分比(0-100)
     */
    @JvmStatic
    @BindingAdapter("marginTopPercent")
    fun setMarginTopPercent(view: TextView, marginPercent: Int) {
        val parent = view.parent as? android.view.ViewGroup ?: return
        val layoutParams = view.layoutParams as? android.view.ViewGroup.MarginLayoutParams ?: return
        
        // 计算父容器高度的百分比
        val actualMargin = (parent.height * marginPercent / 100)
        
        // 更新布局参数
        if (layoutParams.topMargin != actualMargin) {
            layoutParams.topMargin = actualMargin
            view.layoutParams = layoutParams
        }
    }
    
    /**
     * 将百分比下边距转换为实际像素
     * @param view TextView控件
     * @param marginPercent 下边距百分比(0-100)
     */
    @JvmStatic
    @BindingAdapter("marginBottomPercent")
    fun setMarginBottomPercent(view: TextView, marginPercent: Int) {
        val parent = view.parent as? android.view.View ?: return
        val layoutParams = view.layoutParams as? android.view.ViewGroup.MarginLayoutParams ?: return
        val actualMargin = (parent.height * marginPercent / 100)
        layoutParams.bottomMargin = actualMargin
        view.layoutParams = layoutParams
    }
} 